#!/usr/bin/perl -w
# Set up /etc/rsyncd.conf
# vim:tw=100 sw=2 expandtab ft=perl

return unless i_has('rsync');
package_check("rsync");

if (-f '/etc/xinetd.d/rsync') {
  file_modify('/etc/xinetd.d/rsync', '/etc/init.d/xinetd reload',
              's/disable\s*=\s*yes/disable = no/');
} elsif (-f '/etc/default/rsync') {
  file_modify('/etc/default/rsync', '/etc/init.d/rsync restart',
              's/RSYNC_ENABLE=false/RSYNC_ENABLE=true/');
}

my %users;
my $config = "";

foreach (i_isa_fetchall("rsync")) {
  while (my($name, $r) = each %$_) {
    next unless $r->{path};
    $r->{uid} ||= 'root';
    $r->{user} = 'backup' unless defined $r->{user};
    $config .= "[$name]\n";
    $config .= "  comment = $r->{comment}\n" if $r->{comment};
    $config .= "  path = $r->{path}\n";
    $config .= "  auth users = $r->{user}\n" if $r->{user};
    $config .= "  hosts allow = $r->{allow}\n" if $r->{allow};
    $config .= "  secrets file = /etc/rsync.secrets\n" if $r->{user};
    $config .= "  uid = $r->{uid}\n" if $r->{uid};
    $config .= "  gid = $r->{gid}\n" if $r->{gid};
    $config .= "  use chroot = $r->{chroot}\n" if $r->{chroot};
    $config .= "  read only = yes\n" unless $r->{readonly};
    $config .= "\n";

    $users{$r->{user}} = $r->{password} if $r->{user} && $r->{password};
  }
}

my $restart = 0;
text_install("/etc/rsyncd.conf", $config) and $restart++ if $config;
text_install("/etc/rsync.secrets", join("", map { "$_:$users{$_}\n" } keys %users), undef,
             { mode => 0600 }) and $restart++ if keys %users;
command("/etc/init.d/rsync restart") if -f '/etc/init.d/rsync' && $restart;

